package cn.edu.sdjzu.xg.bysj.service;
//201902104061徐中堂
import cn.edu.sdjzu.xg.bysj.dao.GraduateProjectStatusDao;
import cn.edu.sdjzu.xg.bysj.domain.GraduateProjectStatus;
import util.JdbcHelper;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;

public class GraduateProjectStatusService {
    //获取 GraduateProjectStatusDao实例
    private GraduateProjectStatusDao graduateProjectStatusDao= GraduateProjectStatusDao.getInstance();
    //新建 GraduateProjectService对象
    private static GraduateProjectStatusService graduateProjectSourceService = new GraduateProjectStatusService();
    //构造器
    private GraduateProjectStatusService(){}
    //获取GraduateProjectService对象
    public static GraduateProjectStatusService getInstance(){
        return graduateProjectSourceService;
    }
    /**
     * 根据id查找GraduateProject信息
     *
     * @param id
     *
     * @return
     * @throws SQLException
     */
    public GraduateProjectStatus find(int id) throws SQLException {
        //获取数据库链接
        Connection connection = JdbcHelper.getConn();
        GraduateProjectStatus graduateProjectStatus =null;
        //关闭自动提交，事务开始
        connection.setAutoCommit(false);
        try {
            graduateProjectStatus = graduateProjectStatusDao.find(connection,id);
            //提交事务
            connection.commit();
        }
        catch (SQLException e) {
            //回滚事务中所有操作
            connection.rollback();
        }
        finally {
            //将自动提交设置为true，结束事务
            connection.setAutoCommit(true);
            //释放连接
            JdbcHelper.close(connection);
        }
        return graduateProjectStatus;
    }
    /**
     * 查询所有
     *
     * @return
     * @throws SQLException
     */
    public Collection<GraduateProjectStatus> findAll() throws SQLException {
        Connection connection =  JdbcHelper.getConn();
        Collection<GraduateProjectStatus> graduateProjectStatuses =null;
        //关闭自动提交，事务开始
        connection.setAutoCommit(false);
        try {
            graduateProjectStatuses = graduateProjectStatusDao.findAll();
            //提交事务
            connection.commit();
        }
        catch (SQLException e) {
            //回滚事务中所有操作
            connection.rollback();
        }
        finally {
            //将自动提交设置为true，结束事务
            connection.setAutoCommit(true);
            //释放连接
            JdbcHelper.close(connection);
        }
        return  graduateProjectStatuses;
    }
}
